package com.yanqu.road.entity.log.maidian;

import com.yanqu.road.entity.log.maidian.impl.TokenBuyMoneyPrice;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * wmm
 * 运营日志上报数数平台需要继承的类
 */
public abstract class BaseLogEntity {

    static Map<Class<? extends BaseDataAttributeCustomGet>, BaseDataAttributeCustomGet> customGetMap = new ConcurrentHashMap<>();

    static {
        customGetMap.put(TokenBuyMoneyPrice.class, new TokenBuyMoneyPrice());
    }

    public abstract long getUserId();

    Map<String, Object> commonDataMap;

    Map<String, Object> attachDataMap;

    int attributeNum;

    public Map<String, Object> getCommonDataMap() {
        return commonDataMap;
    }

    public void setCommonDataMap(Map<String, Object> commonDataMap) {
        if(this.commonDataMap == null) {
            this.commonDataMap = commonDataMap;
        }else {
            this.commonDataMap.putAll(commonDataMap);
        }
    }

    protected ILogTraceBuilder traceBuilder;

    public void buildCommonData(){
        traceBuilder.buildCommonData(this);
        if(commonDataMap != null){
            if(!commonDataMap.containsKey("#time")) {
                commonDataMap.put("#time", DateHelper.getCurrentDateTimeString(System.currentTimeMillis()));
            }
        }
    }

    public void build(String attributeName, Object object){
        if(commonDataMap == null){
            commonDataMap = new HashMap<>();
        }
        if(StringUtils.isNullOrEmpty(attributeName)){
            return;
        }
        if(null == object){
            return;
        }
        if(!commonDataMap.containsKey("#time")) {
            commonDataMap.put("#time", DateHelper.getCurrentDateTimeString(System.currentTimeMillis()));
        }
        commonDataMap.put(attributeName, object);
        attributeNum++;
    }

    public void trace(String eventName){
        if(commonDataMap != null) {
            if(attributeNum == 0){
                return;
            }
            if(attachDataMap != null){
                commonDataMap.putAll(attachDataMap);
            }
            traceBuilder.trace(eventName, commonDataMap);
        }
    }

    public void setTraceBuilder(ILogTraceBuilder traceBuilder) {
        this.traceBuilder = traceBuilder;
    }

    /**
     * 额外附加的数据
     */
    public void setAttachDataMap(Map<String, Object> attachDataMap) {
        this.attachDataMap = attachDataMap;
    }

    /**
     * 自定义的数据转化
     */
    public void customGet(Class<? extends BaseDataAttributeCustomGet> clazz, String attributeName){
        if(clazz.equals(BaseDataAttributeCustomGet.class)){
            return;
        }
        BaseDataAttributeCustomGet customGet = customGetMap.get(clazz);
        if(customGet != null){
            try {
                customGet.get(this, attributeName);
            }catch (Exception e){

            }
        }
    }

}
